package com.itxh.core.config;

import com.itxh.core.excutor.*;
import lombok.Data;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 封装整体配置的类
 */
@Data
public class Configuration {
    private DataSource dataSource;
    private Map<String,MappedStatement> mappedStatements = new HashMap<>();

    // 二级缓存是否开启的标记，默认是true
    private boolean cacheEnabled = true;

    public void addMappedStatement(String statementId,MappedStatement statement){
        this.mappedStatements.put(statementId, statement);
    }

    public MappedStatement getMappedStatementById(String statementId){
        return this.mappedStatements.get(statementId);
    }

    public Executor newExecutor(String executorType){
        executorType = executorType == null ? "simple": executorType;

        // 去创建真正的执行器
        Executor executor = null;
        if (executorType.equals("simple")){
            executor = new SimpleExecutor();
        }else if (executorType.equals("batch")){
            executor = new BatchExecutor();
        }else if (executorType.equals("reuse")){
            executor = new ReuseExecutor();
        }

        if (cacheEnabled){
            executor = new CachingExecutor(executor);
        }

        return executor;
    }
}
